package common.event;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import common.util.MathUtil;

import android.util.Log;

/**
 * 
 * @author Administrator
 * "bv":{"command":"login","containerId":"loginform"",containerId1":"loginform","containerId1":"loginform","containerId1":"loginform"}
 * "bv":{"command":"login","params":{"containerId":"loginform"",containerId1":"loginform","containerId1":"loginform","containerId1":"loginform"},"":""}
 */

public class Behavior //implements Cloneable
{
	private String command;
	@SuppressWarnings("unchecked")
	private Map paramMap=null; 
	
//	@Override  
//    public Object clone() throws CloneNotSupportedException 
//    {   		
//		Behavior bv=(Behavior)super.clone();   
//		bv.addParam(paramMap);
//		return bv;
//    }    
//	
//	public void addParam(Map paraMap)
//	{
//		if(paramMap==null)
//		{
//			paramMap=new HashMap();
//		}
//		Set set=paraMap.entrySet();
//		Iterator it=set.iterator();
//		while(it.hasNext())
//		{
//			Entry en=(Entry)it.next();
//			String key=(String)en.getKey();
//			Object obj=en.getValue();
//			paramMap.put(key, obj);
//		}
//	}
	
	public Behavior(String command)
	{
		this.command=command;
	}
	
	public void addParam(String key,Object value)
	{
		if(paramMap==null)
		{
			paramMap=new HashMap();
		}
		
		if(paramMap.containsKey(key))
		{
			paramMap.remove(key);
		}
		paramMap.put(key, value);
	}
	
	public void addParam(String key,int value)
	{
		Integer obj=Integer.valueOf(value);
		addParam(key,obj);
	}
	
	public void addParam(String key,boolean value)
	{
		Boolean obj=Boolean.valueOf(value);
		addParam(key,obj);
	}
	
	public void addParam(String key,String value)
	{
		Object obj=value;
		addParam(key,obj);
	}
	
	public void removeParam(String key)
	{
		if(paramMap!=null)
		{
			paramMap.remove(key);
		}
	}
	
	public Object getParam(String key)
	{
		Object paramValue=null;
		
		if(paramMap!=null)
		{
			paramValue=paramMap.get(key);
		}
		
		return paramValue;
	}
	
	public String getParamString(String key)
	{
		String paramValue=null;
		
		if(paramMap!=null)
		{
			paramValue=(String)paramMap.get(key);
		}
		
		return paramValue;
	}
	
	public int getParamInt(String key,int defalutValue)
	{
		int value=defalutValue;
		
		if(paramMap!=null)
		{
			Object paramObj=paramMap.get(key);
			if(paramObj!=null)
			{
				try
				{
					if(paramObj instanceof Integer)
		    		{
		    			value=((Integer)paramObj).intValue();
		    		}
		    		else if(paramObj instanceof String)
		    		{
		    			value=MathUtil.string2int((String)paramObj,value);
		    		}
				}
				catch(ClassCastException e)
				{
					Log.e("",e.toString());
				}
			}
		}
		
		return value;
	}
	
	public boolean getParamBoolean(String key,boolean defalutValue)
	{
		boolean value=defalutValue;
		
		if(paramMap!=null)
		{
			Object paramObj=paramMap.get(key);
			if(paramObj!=null)
			{
				try
				{
					value=((Boolean)paramObj).booleanValue();
					
					if(paramObj instanceof Boolean)
		    		{
		    			value=((Boolean)paramObj).booleanValue();
		    		}
		    		else if(paramObj instanceof Integer)
		    		{
		    			value=((Integer)paramObj).intValue()==1?true:false;
		    		}
				}
				catch(ClassCastException e)
				{
					Log.e("",e.toString());
				}
			}
		}
		
		return value;
	}
	
	public String getCommand()
	{
		return command;
	}

	public void setCommand(String command)
	{
		this.command = command;
	}

	private String getCommand(String cmdStr)
	{
		String cmd=null;

		return cmd;
	}
}
